package com.stylefeng.guns.modular.system.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.model.Mkdata;
import com.stylefeng.guns.modular.system.service.IMkdataService;
import com.stylefeng.guns.modular.system.warpper.MkdataWarpper;

/**
 * mkdata控制器
 *
 * @author fengshuonan
 * @Date 2018-06-05 13:46:35
 */
@Controller
@RequestMapping("/mkdata")
public class MkdataController extends BaseController {

    private String PREFIX = "/system/mkdata/";

    @Autowired
    private IMkdataService mkdataService;

    /**
     * 跳转到mkdata首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "mkdata.html";
    }

    /**
     * 跳转到添加mkdata
     */
    @RequestMapping("/mkdata_add")
    public String mkdataAdd() {
        return PREFIX + "mkdata_add.html";
    }

    /**
     * 跳转到修改mkdata
     */
    @RequestMapping("/mkdata_update/{mkdataId}")
    public String mkdataUpdate(@PathVariable Integer mkdataId, Model model) {
        Mkdata mkdata = mkdataService.selectById(mkdataId);
        model.addAttribute("item",mkdata);
        LogObjectHolder.me().set(mkdata);
        return PREFIX + "mkdata_edit.html";
    }

    /**
     * 获取mkdata列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String date,@RequestParam(required = false) Integer type) {
    	Page<Mkdata> page = new PageFactory<Mkdata>().defaultPage();
    	if(type == null) type = 1;
    	List<Map<String, Object>> mkdatas = mkdataService.selectMapsByMkData(page,date,type);
    	page.setRecords((List<Mkdata>) new MkdataWarpper(mkdatas).warp());
        return super.packForBT(page);
    }

    /**
     * 新增mkdata
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Mkdata mkdata) {
        mkdataService.insert(mkdata);
        return SUCCESS_TIP;
    }

    /**
     * 删除mkdata
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer mkdataId) {
        mkdataService.deleteById(mkdataId);
        return SUCCESS_TIP;
    }

    /**
     * 修改mkdata
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Mkdata mkdata) {
        mkdataService.updateById(mkdata);
        return SUCCESS_TIP;
    }

    /**
     * mkdata详情
     */
    @RequestMapping(value = "/detail/{mkdataId}")
    @ResponseBody
    public Object detail(@PathVariable("mkdataId") Integer mkdataId) {
        return mkdataService.selectById(mkdataId);
    }
}
